package com.mes.basicdata.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.bstek.dorado.data.provider.Page;
import com.mes.basicdata.dao.iface.IOperationDao;
import com.mes.basicdata.dao.iface.IOperationResourceDao;
import com.mes.basicdata.domain.Operation;
import com.mes.basicdata.domain.OperationResource;
import com.mes.basicdata.domain.Part;
import com.mes.common.dao.BasicDao;

@Repository
public class OperationResourceDao extends BasicDao<OperationResource> implements IOperationResourceDao {
	@Autowired
	private IOperationDao operationDao;

	@Override
	public void getOperationResourceByConditions(Page<Part> page, Map<String, Object> params) throws Exception {
		// TODO Auto-generated method stub
		Map<String, Object> args = new HashMap<String, Object>();
		StringBuffer coreHql = new StringBuffer("from " + Part.class.getName() + " a where 1=1 ");

		String drawingId = (String) params.get("drawingId");
		if (StringUtils.isNotEmpty(drawingId)) {
			coreHql.append(" and a.drawingId like :drawingId ");
			args.put("drawingId", "%" + drawingId + "%");
		}

		String resourceName = (String) params.get("resourceName");
		if (StringUtils.isNotEmpty(resourceName)) {
			coreHql.append(" and aresourceName like :resourceName ");
			args.put("resourceName", "%" + resourceName + "%");
		}

		String resourceDescription = (String) params.get("resourceDescription");
		if (StringUtils.isNotEmpty(resourceDescription)) {
			coreHql.append(" and a.resourceDescription = :resourceDescription ");
			args.put("resourceDescription", Integer.parseInt(resourceDescription));
		}

		String resourceType = (String) params.get("resourceType");
		if (StringUtils.isNotEmpty(resourceType)) {
			coreHql.append(" and a.resourceType = :resourceType ");
			args.put("resourceType", Integer.parseInt(resourceType));
		}

		String countHql = "select count(*) " + coreHql.toString();
		// String hql = coreHql.toString() + "order by a.dept.id,a.id";
		String hql = coreHql.toString();

		Map<String, Object> newArgs = new HashMap<String, Object>();
		String key;
		String newKey;
		Iterator<String> iterator = args.keySet().iterator();
		while (iterator.hasNext()) {
			key = (String) iterator.next();
			newKey = key.replace(".", "_");
			newArgs.put(newKey, args.get(key));
			hql = hql.replace(":" + key, ":" + newKey);
			countHql = countHql.replace(":" + key, ":" + newKey);

			System.out.println("hql: " + hql);
			System.out.println("countHql: " + countHql);
			System.out.println("newArgs: " + newArgs);

			this.pagingQuery(page, hql, countHql, newArgs);
		}
	}

	@Override
	public List<OperationResource> getOperationResourceListByOperationUid(String operationUid) throws Exception {
		// TODO Auto-generated method stub
		Operation operation = operationDao.getById(operationUid);
		List<OperationResource> ors = new ArrayList<OperationResource>();
		Set<OperationResource> operationResources = operation.getOperationResources();
		for (OperationResource or : operationResources) {
			ors.add(or);
		}
		return ors;
	}

	@Override
	public void getOperationResourceListByOperationUid(String string, Page<OperationResource> page) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Integer getResourceCount() {
		String hql = "select count(*) from b_operation_resource bo where bo.opuid=UNIQUEID";
		Query query = this.getSessionFactory().getCurrentSession().createSQLQuery(hql);
		Object result = query.uniqueResult();
		Integer i = Integer.parseInt(String.valueOf(result));
		return i;
	}


}